﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using LX.Core;

namespace S4Store.Domain.CarSales
{
    public class MonthCarSale
    {
        public MonthCarSale()
        {
            CarModelSales = new List<MonthCarModelSale>();
        }

        public int Id { get; set; }
        public int StoreId { get; set; }
        public DateTime Month { get; set; }

        [NotMapped]
        public DateTime CurrentDate { get; set; }

        public virtual Store Store { get; set; }
        [NotMapped]
        public virtual List<MonthCarModelSale> CarModelSales { get; set; }


        [NotMapped]
        [Alias("C")]
        [Description("本月执行天数")]
        public int Days
        {
            get { return DateTime.DaysInMonth(Month.Year, Month.Month); }
        }
        [NotMapped]
        [Alias("I")]
        [Description("已执行天数")]
        public int ExcuteDays
        {
            get { return CurrentDate.Day; }
        }

        [NotMapped]
        [Description("时间进度")]
        public decimal RateOfTime
        {
            get { return ExcuteDays * 100 / Days; }
        }


        /// <summary>
        /// 上月成交率
        /// </summary>
        public decimal? LastMonthVr { get; set; }

        /// <summary>
        /// 上月订交转化率
        /// </summary>
        public decimal? LastMonthOrderTransformRate{ get; set; }

        /// <summary>
        /// 目标订单量
        /// </summary>
        public int TargetOrderCount { get; set; }

        /// <summary>
        /// 精装车销售率目标
        /// </summary>
        public decimal HardcoverCarTarget { get; set; }

        /// <summary>
        /// 保险目标
        /// </summary>
        public decimal InsureTarget { get; set; }

        /// <summary>
        /// 置换目标
        /// </summary>
        public decimal ReplacementTarget { get; set; }

        /// <summary>
        /// 金融目标
        /// </summary>
        public decimal FinancialTarget { get; set; }

        /// <summary>
        /// 延保目标
        /// </summary>
        public decimal ExtendTarget { get; set; }

        /// <summary>
        /// 本月交车总和
        /// </summary>
        [NotMapped]
        public int TotalBYJC
        {
            get
            {
                return this.CarModelSales.Sum(t => t.YJEW + t.YJBD);
            }
        }

        /// <summary>
        /// 本月销售目标
        /// </summary>
        [NotMapped]
        public int TotalXSMB
        {
            get
            {
                return this.CarModelSales.Sum(t => t.XSMB);
            }
        }

        /// <summary>
        /// 交车率
        /// </summary>
        [NotMapped]
        public decimal JCLV
        {
            get
            {
                if (TotalXSMB == 0) return 0;
                return (decimal)TotalBYJC*100 / (decimal)TotalXSMB;
            }

        }


        /// <summary>
        /// 本月订单总和
        /// </summary>
        [NotMapped]
        public int TotalBYDD
        {
            get
            {
                return this.CarModelSales.Sum(t => t.YDBD+t.YDEW);
            }
        }

        /// <summary>
        /// 本月目标订单
        /// </summary>
        [NotMapped]
        public int TotalMBDD
        {
            get
            {
                if(LastMonthOrderTransformRate==null||LastMonthOrderTransformRate==0)return 0;
                return (int)((decimal)TotalXSMB / LastMonthOrderTransformRate*100);
            }
        }
        
        /// <summary>
        /// 本月订单进度
        /// </summary>
        [NotMapped]
        public decimal DDJD
        {
            get
            {
                if(TotalMBDD==0)return 0;
                return (decimal)(TotalBYDD + this.CarModelSales.Sum(t => t.SYWJ))*100 / (decimal)TotalMBDD;
            }
        }

        
    }
}
